package com.leetroll.app.controller;

import com.leetroll.api.service.ItemService;
import com.leetroll.api.service.UserActionService;
import com.leetroll.app.annotation.OperationLogger.OperationLogger;
import com.leetroll.common.bean.ApiResponse;
import com.leetroll.common.entity.User;
import com.leetroll.common.enums.EModule;
import com.leetroll.common.enums.EUserAction;
import com.leetroll.common.vo.ItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/item")
@Api(value = "SQL搜索相关接口", tags = {"SQL搜索相关接口"})
@Slf4j
public class ItemController {
    private final ItemService itemService;
    private final UserActionService actionService;

    @Autowired
    public ItemController(ItemService itemService, UserActionService actionService) {
        this.itemService = itemService;
        this.actionService = actionService;
    }

    @ApiOperation(value = "item详情", notes = "item详情")
    @GetMapping("/get/{id}")
    public ApiResponse<ItemVO> get(@PathVariable Integer id) {
        ItemVO itemVO = itemService.getAllInfoById(id);
        return ApiResponse.OK(itemVO);
    }

    //todo: 有多个附件的资源需要单独处理
    @ApiOperation(value = "下载item附件", notes = "下载item附件")
    @OperationLogger(modul = EModule.ITEM,type = "ITEM_DOWNLOAD",desc = "附件下载")
    @GetMapping("/download-attach/{itemId}")
    public ApiResponse<String> downloadAttachment(@PathVariable Integer itemId) {
        String link = itemService.downloadAttachment(itemId);
        return ApiResponse.OK(link);
    }

    @ApiOperation(value = "下载item附件", notes = "下载item附件")
    @OperationLogger(modul = EModule.ITEM,type = "ITEM_DOWNLOAD",desc = "附件下载")
    @GetMapping("/download/{itemId}")
    public ApiResponse<String> download(@PathVariable Integer itemId) {
        return ApiResponse.OK();
    }

    @ApiOperation(value = "查询用户点赞收藏的items", notes = "查询用户点赞收藏的items")
    @GetMapping("/listByActionAndUser")
    public ApiResponse<List<ItemVO>> listByActionAndUser(@RequestParam(name = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                                         @RequestParam(name = "pageSize", required = false, defaultValue = "12") Integer pageSize,
                                                         @RequestParam(name = "action") String action) {
        List<ItemVO> itemVOList = itemService.listByActionAndUser(action, pageNumber, pageSize);
        return ApiResponse.OK(itemVOList);
    }

    //todo 返回一个map: 按action单个查询请求数太多
    @ApiOperation(value = "查询用户某个action下的item数量", notes = "查询用户某个action下的item数量")
    @GetMapping("/countByActionAndUser")
    public ApiResponse<Integer> countByActionAndUser(@RequestParam(name = "action") String action) {
        int count = actionService.countItemsByActionAndUser(action);
        return ApiResponse.OK(count);
    }

    @ApiOperation(value = "查询某个item下的用户action数量", notes = "查询某个item下的用户action数量")
    @GetMapping("/countByAction")
    public ApiResponse<Map<String, Integer>> countByAction(@RequestParam(name = "itemId") Long itemId) {
        int likes = actionService.countItemsByAction(itemId, EUserAction.LIKE.getAction());
        int collects = actionService.countItemsByAction(itemId, EUserAction.COLLECT.getAction());
        Map<String, Integer> map = new HashMap<String, Integer>() {{
            put(EUserAction.LIKE.getAction(), likes);
            put(EUserAction.COLLECT.getAction(), collects);
        }};
        return ApiResponse.OK(map);
    }


    @ApiOperation(value = "item信息更新(前端调试)", notes = "item信息更新", response = String.class)
    @GetMapping("/update-price")
    public ApiResponse<User> updateItemPrice(@RequestParam(name = "id") Integer id,
                                             @RequestParam(name = "price") Double price) {
        itemService.updatePrice(id, price);
        return ApiResponse.OK();
    }

    @ApiOperation(value = "item信息更新(前端调试)", notes = "item信息更新", response = String.class)
    @GetMapping("/update-tag")
    public ApiResponse<User> updateItemPrice(@RequestParam(name = "id") Integer id,
                                             @RequestParam(name = "tag") String tag) {
        itemService.updateTag(id, tag);
        return ApiResponse.OK();
    }

}
